package cn.gwm.flink.streaming.dwm.charge.assist.gps;

import cn.gwm.flink.streaming.constant.BaseFields;
import cn.gwm.flink.streaming.dwm.charge.bean.ChargeArgs;
import cn.gwm.flink.streaming.dwm.charge.util.LastTripTimeUtil;
import cn.hutool.json.JSONObject;

/**
 * @ClassName TripGpsMatch
 * @Description 最后的行程中进行匹配
 * @Author LiangGuang
 * @Date 2023/06/28 15:25
 */
public class TripGpsMatch extends GpsMatch {
    private final static String GPS_FLAG = "gpsflag";

    @Override
    public void match(JSONObject summar, ChargeArgs args) throws Exception {
        if (super.getNext() != null) {
            super.getNext().match(summar, args);
        }
        if (!summar.containsKey(BaseFields.LATITUDE) || BaseFields.defaultVal.equals(summar.getStr(BaseFields.LATITUDE))) {
            if (args.getTripRedis()) {
                JSONObject nowdata = args.getRealNowData();
                JSONObject lastTrip = LastTripTimeUtil.lastTrip(nowdata.getLong(BaseFields.tid), LastTripTimeUtil.reloadTrip(nowdata.getStr(BaseFields.vin)));
                if (lastTrip.size() > 0) {
                    if (lastTrip.containsKey(GPS_FLAG) && lastTrip.getInt(GPS_FLAG) == 1) {
                        // 可信
                        summar.set(BaseFields.LATITUDE, lastTrip.get(BaseFields.LATITUDE));
                        summar.set(BaseFields.LONGITUDE, lastTrip.get(BaseFields.LONGITUDE));
                    }
                }
            }
        }
    }
}
